﻿using System.Collections;

namespace Rules
{
    public class MemberOfRule : RuleBase<object>
    {
        public MemberOfRule(IEnumerable list, string propertyName)
            : base(propertyName)
        {
            List = list;
        }

        public MemberOfRule(IEnumerable list, string propertyName, string description)
            : base(propertyName, description)
        {
            List = list;
        }

        public IEnumerable List { get; private set; }

        protected override bool DoEvaluate(object subject)
        {
            return GetList(List).Contains(subject);
        }

        protected override string GetDefaultDescription()
        {
            return string.Format("{0} must be an item in the list", PropertyName);
        }

        private ArrayList GetList(IEnumerable items)
        {
            ArrayList container = new ArrayList();
            foreach (object item in items)
            {
                container.Add(item);
            }
            return container;
        }
    }
}